package com.shxc.smarttrade.old.activity;

import android.Manifest;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.shxc.smarttrade.R;
import com.shxc.smarttrade.old.utils.BaseUtils;
import com.umeng.analytics.MobclickAgent;

import butterknife.BindView;
import butterknife.OnClick;
import kr.co.namee.permissiongen.PermissionFail;
import kr.co.namee.permissiongen.PermissionGen;
import kr.co.namee.permissiongen.PermissionSuccess;

/**
 * Created by jcdev1 on 2017/7/13.
 */

public class AnalySisGuideActivity extends BaseActivity implements View.OnLongClickListener {
    @BindView(R.id.tv_center)
    TextView tvCenter;
    @BindView(R.id.iv_service)
    ImageView ivService;
    @BindView(R.id.iv_official)
    ImageView ivOfficial;
    private int tag = 0;//标志当前长按对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_analysis_guide);
        tvCenter.setText("行情交流");
        initListener();
    }

    private void initListener() {
        ivService.setOnLongClickListener(this);
        ivOfficial.setOnLongClickListener(this);
    }

    @OnClick({R.id.rl_left, R.id.btn_copy_service, R.id.iv_service, R.id.btn_official, R.id.iv_official})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_left:
                finish();
                break;
            case R.id.btn_copy_service:
                cliboardText("mayiforex");
                break;
            case R.id.btn_official:
                cliboardText("TradeMaster外汇交易大师");
                break;
        }
    }


    @Override
    public boolean onLongClick(View v) {
        switch (v.getId()) {
            case R.id.iv_service:
                tag = 0;
                PermissionGen.with(this)
                        .addRequestCode(100)
                        .permissions(
                                Manifest.permission.READ_EXTERNAL_STORAGE,
                                Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        .request();
                break;
            case R.id.iv_official:
                tag = 1;
                PermissionGen.with(this)
                        .addRequestCode(100)
                        .permissions(
                                Manifest.permission.READ_EXTERNAL_STORAGE,
                                Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        .request();
                break;
        }
        return false;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                           int[] grantResults) {
        PermissionGen.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }

    @PermissionSuccess(requestCode = 100)
    public void doSomething() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示");
        builder.setMessage("是否保存图片？");
        builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                Bitmap bitmap;
                if (tag == 0)
                    bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.service_code);
                else
                    bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.official_code);
                boolean saveStatus = BaseUtils.saveImageToGallery(getApplicationContext(), bitmap);
                if (saveStatus)
                    showToast("保存成功");
                else
                    showToast("保存失败");
            }
        });

        builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.show();
    }

    @PermissionFail(requestCode = 100)
    public void doFailSomething() {
        showToast("请在设置中打开权限");
    }

    /**
     * 粘贴
     *
     * @param text
     */
    private void cliboardText(String text) {
        ClipboardManager clipboard = (ClipboardManager)
                getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText("webChart", text);
        clipboard.setPrimaryClip(clip);
        showToast("复制成功");
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);

    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
